<?php

namespace App\Admin\Controllers;

use App\Models\PrintArea;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class PrintAreaController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '打印机设置';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new PrintArea());
        
        $grid->column('name', __('Name'));
        $grid->column('reception_machine_id', '打印机机器号（英文逗号隔开）');
        $grid->column('type', '模式')->using([0 => '总单+分单', 1 => '仅总单', 2 => '仅分单']);
        $grid->column('created_at', __('Created at'));
        
        $grid->filter(function (Grid\Filter $filter) {
            $filter->disableIdFilter();
            $filter->like('name', __('Name'));
            $filter->equal('type', '模式')->radio(['' => '全部', 1 => '仅总单', 2 => '仅分单']);
        });

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(PrintArea::findOrFail($id));
        
        $show->field('name', __('Name'));
        $show->field('description', __('Description'));
        $show->field('reception_machine_id', '打印机机器号（英文逗号隔开）');
        $show->field('created_at', __('Created at'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new PrintArea());

        $form->hidden('store_id', __('Store id'))->default(Admin::user()->store_id);
        $form->text('name', __('Name'))->required();
        $form->text('reception_machine_id', '打印机机器号（英文逗号隔开）')->required();
        $form->radio('type', '模式')->default(1)->options([ 1 => '仅总单', 2 => '仅分单'])->required();
        $form->radio('is_cashier', '是否打印交班小票')->options([1 => '是', 2 => '否'])->default(2);
        $form->textarea('description', __('Description'));
        
        return $form;
    }
}
